package com.pfzy.utildemo.activity.load.protocol;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.pfzy.utildemo.R;
import com.pfzy.lib.config.AppConfig;

/**
 * 隐私协议管理器
 */
public class PrivacyProtocolManager {
    private static volatile PrivacyProtocolManager sManager;
    private ILibrariesInit mLibrariesInit;

    public static PrivacyProtocolManager getInstance() {
        if (sManager == null) {
            synchronized (PrivacyProtocolManager.class) {
                if (sManager == null) {
                    sManager = new PrivacyProtocolManager();
                }
            }
        }
        return sManager;
    }

    private PrivacyProtocolManager() {
    }

    public void setInitListener(ILibrariesInit listener) {
        this.mLibrariesInit = listener;
        if (AppConfig.isPrivacyProtocolAgreed()) {
            initLibraries();
        }
    }

    public void initLibraries() {
        if (this.mLibrariesInit != null) {
            this.mLibrariesInit.initLibraries();
        }
    }

    public void showPrivacyDialog(final Activity activity, final FrameLayout layout, final PrivacyProtocolAgreedListener listener) {
        if (layout == null) {
            initLibraries();
            listener.agreed();
            return;
        }
        View contentView = LayoutInflater.from(activity).inflate(R.layout.dialog_privacy_protocol, layout, false);
        layout.addView(contentView);
        TextView tvContent = contentView.findViewById(R.id.tv_content);
        setContentText(activity, tvContent);
        contentView.findViewById(R.id.btn_agree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout.setVisibility(View.GONE);
                AppConfig.setPrivacyProtocolStatus(true);
                initLibraries();
                listener.agreed();
            }
        });
        contentView.findViewById(R.id.btn_not_agree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!activity.isFinishing()) {
//                    DialogUtil.show(activity, "您需要同意本隐私协议才可继续使用曹操司机经典版", "", "退出应用", "查看协议", new DialogUtil.ClickListener() {
//                        @Override
//                        public void onLeftClicked() {
//                            try {
//                                activity.finish();
//                                System.exit(0);
//                            } catch (Throwable e) {
//                                e.printStackTrace();
//                            }
//                        }
//
//                        @Override
//                        public void onRightClicked() {
//                        }
//                    });
                }
            }
        });
        layout.setVisibility(View.VISIBLE);
    }

    private void setContentText(final Context context, TextView tvContent) {
        String privacyContent = context.getResources().getString(R.string.compat_privacy_content);
        SpannableString spannable = new SpannableString(privacyContent);
        try {
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (!ClickUtils.isFastClick()) {
//                        try {
//                            Uri uri = Uri.parse(CaocaoEnvironment.CAOCAO_H5 + ConstantsH5URI.DRIVER_PRIVACY_PROTOCOL);
//                            Intent intent = new Intent("android.intent.action.VIEW", uri);
//                            context.startActivity(intent);
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            NavigationPageProcessor.process(ConstantsH5URI.DRIVER_PRIVACY_PROTOCOL);
//                        }
//                    }
                }
            };
            int start = privacyContent.indexOf("《隐私政策》");
            int end = start + "《隐私政策》".length();
            int color = Color.parseColor("#1BB31B");
            spannable.setSpan(new ForegroundColorSpan(color), start, end, 33);
//            spannable.setSpan(new CommonClickSpan(color, listener), start, end, 33);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tvContent.setText(spannable);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
